#include "widget.h"
#include <QTime>
#include <QtMath>
#include <QPainter>

Widget::Widget(QWidget *parent)
    : QWidget(parent)
{
    index = 0;
    timer = new QTimer(this);
    connect(timer, SIGNAL(timeout()), this, SLOT(timerout()));
    timer->start(20);

//    slider = new QSlider(this);
//    slider->setGeometry(10,10,400,20);
//    slider->setOrientation(Qt::Horizontal);  // 水平方向
//    slider->setMinimum(0);  // 最小值
//    slider->setMaximum(150);  // 最大值
//    slider->setSingleStep(1);  // 步长

//    connect(slider,SIGNAL(valueChanged(int)),this,SLOT(sliderslot(int)));

}

Widget::~Widget()
{

}

void Widget::sliderslot(int val)
{
    index = val;
    //++index;
    if(index>140)
        index=0;
    update();
}

void Widget::timerout()
{
    ++index;
    if(index>140)
        index=0;
    update();
}

QRectF Widget::textRectF(double radius, int pointSize, double angle)
{
    QRectF rectF;
    rectF.setX(radius*qCos(angle*M_PI/180.0) - pointSize*2);
    rectF.setY(radius*qSin(angle*M_PI/180.0) - pointSize/2.0);
    rectF.setWidth(pointSize*4);
    rectF.setHeight(pointSize);
    return rectF;
}

void Widget::paintEvent(QPaintEvent * event)
{
    Q_UNUSED(event);
    QFont font("Arial Rounded MT Bold");

    // 指针形状 - 多边形
    static const QPoint secondHand[4] = {
        QPoint(7, -12),//Y值控制指针在圆环的位置
        QPoint(-7, -12),
        QPoint(-1, -95),
        QPoint(1, -95)
    };

    // 指针及其他位置颜色
    QColor backgroundColor(94, 94, 94, 255);//圆盘背景颜色
    QColor redHandleColor(255, 0, 0);//指针颜色
    QColor secondColor(0, 0, 0, 150);//10步长的刻度颜色 黑
    QColor minuteColor(255, 255, 255, 150);//小格的刻度颜色
    QColor halfColor(255, 255, 255, 150);//半格的刻度颜色 白

    int side = qMin(width(), height());
    QPainter painter(this);
    painter.setRenderHint(QPainter::Antialiasing);
    // 平移坐标系原点至中心点
    painter.translate(width() / 2, height() / 2);
    // 缩放 //控制刻度的半径
    painter.scale(side / 300.0, side / 300.0);

    //绘制背景黑色圆盘 backgroundColor
    QPen bgpen;
    bgpen.setColor(backgroundColor);
    bgpen.setWidth(120);
    painter.setPen(bgpen);
    painter.drawArc(-40,-40,80,80,0,5760);

   

    //中心画一个红色圆环
    QPen pen;
    pen.setColor(QColor(255,0,0));
    pen.setWidth(10);
    painter.setPen(pen);
    painter.drawArc(-10,-10,20,20,0,5760);

    painter.setPen(secondColor);
    // 绘制刻度线 （270度 / 15 = 18度）每大格间距18度
    for (int i = 0; i < 20; ++i){
        if(i>=3 && i<=7){
        }else{
            painter.drawLine(88, 0, 96, 0);
        }
        painter.rotate(18);//按给定角度进行顺时针旋转
    }
    //设置字体大小
    int radius = 95;//数字显示半径值
    //QFont font = painter.font();
    font.setBold(true);
    painter.setFont(font);
    int pointSize = font.pointSize();

    //绘制速度单位文字 Km/h
    painter.drawText(QRect(-25,-50,50,15), Qt::AlignCenter, QString("Km/h"));

    // 绘制文本
    for (int i = 0; i < 20; ++i) {
        if(i>=3 && i<=7){
            continue;
        }else{
            if(i>=0 && i<=2)
                painter.drawText(textRectF(radius*0.8, pointSize, i * 18), Qt::AlignCenter, QString::number((i+12)*10));
            else
                painter.drawText(textRectF(radius*0.8, pointSize, i * 18), Qt::AlignCenter, QString::number((i-8)*10));
        }
    }

    painter.setPen(minuteColor);
    // 绘制内刻度线 每格间隔1.8度
    for (int j = 0; j < 200; ++j) {
        if(j>20 && j<80){

        }else{
            if ((j % 5) != 0)//半格处预留
                painter.drawLine(92, 0, 96, 0);
        }
        painter.rotate(1.8);
    }
    // 绘制内刻度线半格刻度
    painter.setPen(halfColor);
    for (int j = 0; j < 200; ++j) {
        if(j>20 && j<80){

        }else{
            if ((j % 5) == 0 && (j % 10) != 0)
                painter.drawLine(90, 0, 96, 0);//长度比最短的刻度长一点点 黑色
        }
        painter.rotate(1.8);
    }

    //下方中部画一个圆角矩形用来显示当前速度
    painter.setBrush(Qt::NoBrush);
    painter.drawRoundedRect(QRectF(-25,60,50,25),6,6);

    //显示当前速度
    painter.drawText(QRectF(-20,62,40,20), Qt::AlignCenter, QString::number(index));

    // 绘制指针
    painter.setPen(Qt::NoPen);
    painter.setBrush(redHandleColor);
    painter.save();
    //一共270度，分150格，每一格1.8度
    painter.rotate(234 + index*1.8);//180+(30*1.8)
    painter.drawConvexPolygon(secondHand, 4);
    painter.restore();
}
